package com.depth.viewer.loader
{
	import com.depth.viewer.container.IWidgetContainer;
	import com.depth.viewer.container.ViewerContainer;
	import com.depth.viewer.container.definition.IWidgetContainerDefinition;
	import com.depth.viewer.facade.IocAppFacade;
	import com.depth.viewer.loader.cache.manager.ModuleCacheProxy;
	import com.depth.viewer.utils.ProgressBarLoader;
	import com.depth.viewer.widget.IBaseWidget;
	import com.depth.viewer.widget.definition.IBaseWidgetDefinition;
	import com.depth.viewer.widget.definition.IWidgetDefinition;
	
	import flash.system.ApplicationDomain;
	import flash.utils.ByteArray;
	import flash.utils.getDefinitionByName;
	
	import mx.core.IFlexModuleFactory;
	import mx.core.IVisualElement;
	import mx.events.ModuleEvent;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleManager;
	
	public class WidgetLoader implements IWidgetLoader
	{
		private var wgtInfo:IModuleInfo;
		private var _loadedHandler:Function;
		private var _loadFailHandler:Function;
		private var _unloadedHandler:Function;
		private var _factory:IFlexModuleFactory;
		public function WidgetLoader()
		{
		}
		
		public function load(widgetDef:IWidgetDefinition,moduleFactory:IFlexModuleFactory,isUserCaceh:Boolean = true):void
		{
			var encryptedLocalStore:Object;
			try {
				encryptedLocalStore = getDefinitionByName("flash.data.EncryptedLocalStore") as Class;
			}catch (e:Error)
			{
				//判断是在air环境中还是在浏览器环境中
			}
			if(isUserCaceh&&encryptedLocalStore==null){
				ModuleCacheProxy.getInstance().load(widgetDef.id,widgetDef.url,
					function(data:ByteArray):void{
						loadWidget(widgetDef,moduleFactory,data);
					});
			}else{
				loadWidget(widgetDef,moduleFactory,null);
			}
		}
		public function loadWidget(widgetDef:IWidgetDefinition,moduleFactory:IFlexModuleFactory,data:ByteArray = null):void
		{
			wgtInfo = ModuleManager.getModule(widgetDef.url);
			wgtInfo.data = widgetDef;
			_factory = moduleFactory;
			wgtInfo.addEventListener(ModuleEvent.READY, widgetReadyHandler);
			wgtInfo.addEventListener(ModuleEvent.ERROR, widgetLoadErrorHandler);
			wgtInfo.load(ApplicationDomain.currentDomain,null,data,moduleFactory);
			wgtInfo.addEventListener(ModuleEvent.UNLOAD,widgetUnloadHandler);
			wgtInfo.addEventListener(ModuleEvent.PROGRESS,widgetProgressHandler);
			trace("-------------------------------------------------------------------正在加载模块数据 : " + widgetDef.url);

		}
		public function unload(widgetDef:IWidgetDefinition):void{
			wgtInfo = ModuleManager.getModule(widgetDef.url);
			wgtInfo.data = widgetDef;
			wgtInfo.release(); 
			wgtInfo.unload();
		}
		
		public function set loadedHandler(handler:Function):void
		{
			this._loadedHandler = handler;
		}
		public function set loadFailHandler(handler:Function):void{
			this._loadFailHandler = handler;
		}
		public function set unloadedHandler(handler:Function):void{
			this._unloadedHandler = handler;
		}
		private function widgetProgressHandler(event:ModuleEvent):void{
			var def:IBaseWidgetDefinition = event.module.data as IBaseWidgetDefinition;
			if(def){
				//trace("模块:"+ def.label + "(" + def.id + ")   " + "己加载 :" + event.bytesLoaded*1024 + "KB  共:" + event.bytesTotal*1024 + "KB");
				ViewerContainer.dispatchEvent(event);
			}
		}
		
		private function widgetReadyHandler(event:ModuleEvent):void{		
			var info:IModuleInfo = event.module;
			var def:IWidgetDefinition = info.data as IWidgetDefinition;
			var widget:IVisualElement = info.factory.create() as IVisualElement;
			
			
			if(def is IBaseWidgetDefinition){
				configWidget(def as IBaseWidgetDefinition,widget as IBaseWidget);
			}else if(def is IWidgetContainerDefinition){
				configWidgetContainer(def as IWidgetContainerDefinition,widget as IBaseWidget);
			}
			trace("-------------------------------------------------------------------成功加载模块数据 : " + def.url);
			
			this._loadedHandler(def,widget);
			
			ViewerContainer.dispatchEvent(event);
		}
		private function widgetUnloadHandler(event:ModuleEvent):void{
			var info:IModuleInfo = event.module;
			var def:IWidgetDefinition = info.data as IWidgetDefinition;
			this._unloadedHandler(def);
			
			ProgressBarLoader.popupProgressBar(false);
			
			ViewerContainer.dispatchEvent(event);
		}
		private function configWidget(def:IBaseWidgetDefinition,widget:IBaseWidget):void{
			var label:String = def.label;
			
			var wIcon:String = def.icon;
			try{
				widget.widgetIcon = wIcon;
				widget.widgetId = def.id;
				widget.widgetTitle = label;
				
			}catch(err:Error){
				trace(err.message);
				trace(err.getStackTrace());
			}finally{
				
			}
		}
		
		private function configWidgetContainer(def:IWidgetContainerDefinition,widget:IBaseWidget):void
		{
			var container:IWidgetContainer = widget as IWidgetContainer;
			container.configParams = def;
			IBaseWidget(container).widgetId = def.id;
		}
		private function widgetLoadErrorHandler(event:ModuleEvent):void{
			//this._loadFailHandler(event.errorText);
			unload(event.module.data as IWidgetDefinition);
			load(event.module.data as IWidgetDefinition,_factory,false);
			trace("---------------------模块加载失败,重新加载模块--"+event.module.data.id);
			//ViewerContainer.dispatchEvent(event);
		}
	}
}